/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.bookingtype;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * BookingTypeBean
 * ----------------
 * This is the remote interface implementation of Booking Type
 *
 *
 * @author Dany Gunawan
 * @lastmodify 2009-12-05
 */
@Stateless
public class BookingTypeManagerBean implements BookingTypeManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addBookingType(BookingTypeDetailedTO to) throws BookingTypeManagerException {

        try {
        
            BookingTypeEntity bt = new BookingTypeEntity();
            bt.setData(to);
            em.persist(bt);

        } catch (Exception e) {
            throw new BookingTypeManagerException("Cannot add Booking Type", e);
        }
    }

    public void editBookingType(BookingTypeDetailedTO to) throws BookingTypeManagerException {
        try {
         
            BookingTypeEntity bt = em.find(BookingTypeEntity.class, to.getId());
            bt.setData(to);
            em.persist(bt);
            
        } catch (Exception e) {
            throw new BookingTypeManagerException("Cannot edit Booking Type", e);
        }

    }

    public void deleteBookingType(Long id) throws BookingTypeManagerException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<BookingTypeTO> getAllBookingTypes() throws BookingTypeManagerException {

        List<BookingTypeTO> results = null;

        try {
            results = new ArrayList<BookingTypeTO>();
            Query allBookingTypesQuery = em.createNamedQuery("findAllBookingTypes");
            List dbResults = allBookingTypesQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                BookingTypeEntity bt = (BookingTypeEntity) dbResults.get(i);
                results.add(bt.getData());
            }
        } catch (Exception e) {

            throw new BookingTypeManagerException("Cannot get all Booking Types", e);

        }
        return results;

    }

    public List<BookingTypeDetailedTO> getAllDetailedBookingTypes() throws BookingTypeManagerException {

        List<BookingTypeDetailedTO> results = null;

        try {
            results = new ArrayList<BookingTypeDetailedTO>();
            Query allBookingTypesQuery = em.createNamedQuery("findAllBookingTypes");
            List dbResults = allBookingTypesQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                BookingTypeEntity bt = (BookingTypeEntity) dbResults.get(i);
                results.add(bt.getDetailedData());
            }
        } catch (Exception e) {

            throw new BookingTypeManagerException("Cannot get all BookingTypeDetailedTOs", e);

        }
        return results;

    }

    public BookingTypeDetailedTO getBookingType(Long id) throws BookingTypeManagerException {

        BookingTypeEntity bt = null;
        BookingTypeDetailedTO to = null;
        try {
            bt = em.find(BookingTypeEntity.class, id);
            to = bt.getDetailedData();
        } catch (Exception e) {
            throw new BookingTypeManagerException("Cannot get Booking Type by id " + id.longValue(), e);
        }
        return to;
    }
}
